﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;

public partial class WebUserControl_Chothoadon : System.Web.UI.UserControl
{
   
    
    string error = "";
    ClassExecute.ClassExecute dtEx;
    string str = BCDataCenter.Configuration.CurrentConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                dtEx = new ClassExecute.ClassExecute();
                DataTable db=dtEx.Get_Data_DataTable(str,ref error,"k_select_location",CommandType.StoredProcedure,
                    new SqlParameter("@sitecode", Session["sitecode"].ToString()));

                lblLocation.Text = db.Rows[0]["location"].ToString();
                Label3.Text = "Trung Tâm Phân phối : " + db.Rows[0]["sitename"].ToString();

                DateTime dateTest = DateTime.Parse(db.Rows[0]["closedate"].ToString(), new CultureInfo("fr-FR", false));
                dateTest = dateTest.AddDays(double.Parse("1"));
                Label4.Text = dateTest.ToString("dd/MM/yyyy");
            }
            catch (Exception ex)
            {
                BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
                string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
                obj.ErrorLog(ex.Message, method);
            }
        }
    }
    private void RefreshList_Intx(string trntyp)
    {
        gv_hoadon.DataBind();
        dtEx = new ClassExecute.ClassExecute();
        
        if (trntyp != "N")
        {  
            DataTable db = dtEx.Get_Data_DataTable(str,ref error, "get_arhdr", CommandType.StoredProcedure, 
                new SqlParameter("@trntyp", trntyp), 
                new SqlParameter("@invdate",Label4.Text),
                new SqlParameter("@sitecode",Session["sitecode"].ToString()));
            if (db.Rows.Count >= 1)
            {
                gv_hoadon.DataSource = db;
                gv_hoadon.DataBind();
            }
        }
    }
    protected void Ddl_loaihoadon_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            RefreshList_Intx(Ddl_loaihoadon.SelectedValue.ToString());
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void bt_chot_Click(object sender, EventArgs e)
    {
        try
        {
           
            dtEx = new ClassExecute.ClassExecute();
            bool kq = false;
           
            CheckBox c;
            string tranno = "";
           
            int check=0;
            ArrayList lstLocked = new ArrayList();
            ArrayList lstNotPrint = new ArrayList();


            for (int i = 0; i < gv_hoadon.Rows.Count; i++)
            {
                c = (CheckBox)gv_hoadon.Rows[i].FindControl("checkbox1");
                if (c.Checked)
                {
                    tranno = gv_hoadon.Rows[i].Cells[1].Text;
                    if (gv_hoadon.Rows[i].Cells[5].Text == "True" && gv_hoadon.Rows[i].Cells[6].Text == "True")
                    {

                        kq = dtEx.ExecuteSQl_bool(str, CommandType.StoredProcedure, "post_arhdr", ref error,
                            new SqlParameter("@trntyp",Ddl_loaihoadon.SelectedValue.ToString()),
                            new SqlParameter("@location", lblLocation.Text),
                            new SqlParameter("@tranno",tranno),
                            new SqlParameter("@username",Session["username"].ToString()));
                        if (kq)
                        {
                            lstLocked.Add(tranno);
                        }
                       
                    }
                    else
                    {
                        lstNotPrint.Add(tranno);
                    }
                    check++;
                }
            }
            if (check==0)
            {
                uscMsgBox1.AddMessage("Chưa chọn hóa đơn để chốt", uscMsgBox.enmMessageType.Error);
               
            }
            else
            {
                if (lstNotPrint.Count>0)
                {
                    string ketquaNotPrint = "";
                    for (int j = 0; j < lstNotPrint.Count; j++)
                    {
                        ketquaNotPrint = ketquaNotPrint + lstNotPrint[j].ToString() + ",";
                    }
                    uscMsgBox1.AddMessage("Các hóa đơn này chưa in nên không thể chốt:" + ketquaNotPrint.Substring(0,ketquaNotPrint.Length-1), uscMsgBox.enmMessageType.Error);
                }
                if (lstLocked.Count > 0)
                {
                    string ketquaLocked = "";
                    for (int j = 0; j < lstLocked.Count; j++)
                    {
                        ketquaLocked = ketquaLocked + lstLocked[j].ToString() + ",";
                    }
                    uscMsgBox1.AddMessage("Các hóa đơn này đã được chốt:" + ketquaLocked.Substring(0, ketquaLocked.Length - 1), uscMsgBox.enmMessageType.Error);
                }
            }
            RefreshList_Intx(Ddl_loaihoadon.SelectedValue.ToString());
        }
        catch (Exception ex)
        {
            
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
   
}
